package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.prd.config.LoginInterceptor;
import com.prd.dto.OaOfferLetterDTO;
import com.prd.enums.OaBasisTypeEnum;
import com.prd.enums.OaIdentityTypeEnum;
import com.prd.enums.OaOfferLetterStatusTpyeEnum;
import com.prd.model.*;
import com.prd.mapper.OaOfferLetterMapper;
import com.prd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 录用通知书 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2023-12-24
 */
@Service
@Slf4j
public class OaOfferLetterServiceImpl extends ServiceImpl<OaOfferLetterMapper, OaOfferLetterDO> implements OaOfferLetterService {

    @Autowired
    private OaOfferLetterMapper oaOfferLetterMapper;

    @Autowired
    private OaEmployeeService oaEmployeeService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private WorkContactFormService workContactFormService;

    @Autowired
    private WorkReportService workReportService;

    /**
     * 添加录用通知书
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    @Transactional
    public JsonData addOfferLetter(OaOfferLetterDTO oaOfferLetterDTO) {

        OaOfferLetterDO oaOfferLetterDO = oaOfferLetterMapper.selectOne(new LambdaQueryWrapper<OaOfferLetterDO>().eq(OaOfferLetterDO::getName, oaOfferLetterDTO.getReceiver().getName()));

        if (oaOfferLetterDTO.getReceiver() == null) {
            return JsonData.buildError("接收人信息不能为空");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        if (StringUtils.isBlank(oaOfferLetterDTO.getReviewer())) {
            return JsonData.buildError("审核人不能为空");
        }
        if (StringUtils.isBlank(oaOfferLetterDTO.getProcessId())) {
            if (oaOfferLetterDO != null) {
                return JsonData.buildError("该人员已在录用通知流程中");
            }
            //启动工作流
            HashMap<String, Object> map = new HashMap<>(1);
            map.put("starter", loginUser.getName());
            // 开启流程的key，就是流程定义文件里 process 标签的id
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oa_add_offer_letter", map);
            log.info("创建添加录用通知书流程 processId：{}", processInstance.getId());
            //提交第一个节点
            Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
            map.clear();
            map.put("reviewer", oaOfferLetterDTO.getReviewer());
            taskService.complete(task.getId(),map);
            //设置为待审核状态
            oaOfferLetterDTO.setStatus(OaOfferLetterStatusTpyeEnum.TO_BE_REVIEW.getCode());
            oaOfferLetterDTO.setName(oaOfferLetterDTO.getReceiver().getName());
            oaOfferLetterDTO.setTelephone(oaOfferLetterDTO.getReceiver().getTelephone());
            oaOfferLetterDTO.setDept(oaOfferLetterDTO.getReceiver().getDeptName());
            oaOfferLetterDTO.setPosition(oaOfferLetterDTO.getReceiver().getDeptPositionName());
            oaOfferLetterDTO.setPost(oaOfferLetterDTO.getReceiver().getDeptPostName());
            oaOfferLetterDTO.setCreatedId(loginUser.getId());
            oaOfferLetterDTO.setCreator(loginUser.getName());
            oaOfferLetterDTO.setCreatedName(loginUser.getName());
            oaOfferLetterDTO.setCreatedTime(LocalDateTime.now());
            oaOfferLetterDTO.setProcessId(processInstance.getId());
            oaOfferLetterMapper.insert(CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class));
        }else {
            HashMap<String, Object> map = new HashMap<>(1);
            //提交第一个节点
            Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
            map.clear();
            map.put("reviewer", oaOfferLetterDTO.getReviewer());
            taskService.complete(task.getId(),map);
            OaOfferLetterDO letterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            //设置为待审核状态
            letterDO.setStatus(OaOfferLetterStatusTpyeEnum.TO_BE_REVIEW.getCode());
            oaOfferLetterMapper.updateById(letterDO);
        }

        return JsonData.buildSuccess();
    }

    /**
     * 录用通知详情
     * @param id
     * @return
     */
    @Override
    public JsonData detail(String id) {
        //TODO 获取使用周报记录

        //TODO 获取奖惩信息

        //TODO 获取入职承诺书

        //TODO 获取保密协议

        //获取录取通知详情
        OaOfferLetterDO oaOfferLetterDO = oaOfferLetterMapper.selectById(id);
        OaOfferLetterDTO oaOfferLetterDTO = CommonUtil.dtoTransfer(oaOfferLetterDO, OaOfferLetterDTO.class);
        //TODO 封装所有详情信息

        return JsonData.buildSuccess(oaOfferLetterDTO);
    }


    /**
     * 审核意见
     * @param oaEmployeeDTO
     * @return
     */
    @Override
    public JsonData viewerAgree(OaOfferLetterDTO oaEmployeeDTO) {
        Task task = taskService.createTaskQuery().processInstanceId(oaEmployeeDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("approver", oaEmployeeDTO.getApprover());
        map.put("reviewPass", "yes");
        try {
            taskService.complete(task.getId(), map);
            //TODO 保存审核意见
            //更新状态
            OaOfferLetterDO oaOfferLetterDO = oaOfferLetterMapper.selectById(oaEmployeeDTO.getId());
            oaOfferLetterMapper.update(oaOfferLetterDO,new LambdaUpdateWrapper<OaOfferLetterDO>()
                    .set(OaOfferLetterDO::getStatus,OaOfferLetterStatusTpyeEnum.TO_BE_APPROVE.getCode()));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("审核通过失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 撤销
     * @param oaEmployeeDTO
     * @return
     */
    @Override
    public JsonData viewerCancel(OaOfferLetterDTO oaEmployeeDTO) {
        Map<String, Object> map = new HashMap<>();
        //提交给组长的时候，需要指定组长的 id
        map.put("approver", oaEmployeeDTO.getApprover());
        map.put("reviewPass", "no");
        Task task = taskService.createTaskQuery().processInstanceId(oaEmployeeDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaEmployeeDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.REVIEW_REJECT.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("审核不通过失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 批准通过
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData approveAgree(OaOfferLetterDTO oaOfferLetterDTO) {
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        Map<String, Object> map = new HashMap<>();
        //提交给组长的时候，需要指定组长的 id
        map.put("emConfirm", oaOfferLetterDTO.getReceiver().getName());
        map.put("checkResult", "通过");
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.TO_BE_HR_CONFIRM.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("批准通过失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 批准不通过
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData approveReject(OaOfferLetterDTO oaOfferLetterDTO) {
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        Map<String, Object> map = new HashMap<>();
        //提交给组长的时候，需要指定组长的 id
        map.put("reviewer", oaOfferLetterDTO.getCreator());
        map.put("checkResult", "驳回");
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.APPROVE_REJECT.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("批准通过失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职管理列表
     * @return
     */
    @Override
    public JsonData getList() {
        return JsonData.buildSuccess(oaOfferLetterMapper.selectList(new QueryWrapper<>()));
    }


    /**
     * 根据类型查询依据列表
     * @param type
     * @return
     */
    @Override
    public JsonData getBasisListByType(Integer type) {
        Map<Integer,Object> resultMap = new HashMap<>();
        if (type.equals(OaBasisTypeEnum.WORK_ORDER.getCode())) {
            List<WorkOrderDO> workOrderDOS = workOrderService.list();
            resultMap.put(type,workOrderDOS);
        }

        if (type.equals(OaBasisTypeEnum.WORK_REPORT.getCode())) {
            List<WorkReportDO> workReportDOS = workReportService.list();
            resultMap.put(type,workReportDOS);
        }

        if (type.equals(OaBasisTypeEnum.WORK_CONTACT.getCode())) {
            List<WorkContactFormDO> workContactFormDOS = workContactFormService.list();
            resultMap.put(type,workContactFormDOS);
        }

        return JsonData.buildSuccess(resultMap);
    }


    /**
     * 确认入职
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData confirm(OaOfferLetterDTO oaOfferLetterDTO) {
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("employerConfirm", oaOfferLetterDTO.getReceiver().getName());
        map.put("confirm", "yes");
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.TO_BE_CHECK.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("确认入职失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 放弃入职
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData confirmGiveUp(OaOfferLetterDTO oaOfferLetterDTO) {
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("confirm", "no");
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.APPROVE_REJECT.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("确认入职失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }



    /**
     * 人员报到
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData employerConfirm(OaOfferLetterDTO oaOfferLetterDTO) {

        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("hr", oaOfferLetterDTO.getReceiver().getName());
            taskService.complete(task.getId());
            //TODO 入职资料上传

            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.TO_BE_EMPLOYED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职人员确认失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }


    /**
     * 退回
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData callback(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("hrConfirm", "退回");
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("退回失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 撤销
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData cancel(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("hrConfirm", "end");
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.CANCEL.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("人事撤销失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }


    /**
     * 人事确认-发起入职审批
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData hrConfirm(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("hrConfirm", "submit");
            map.put("bookReviewer", oaOfferLetterDTO.getReviewer());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("人事确认-发起入职审批失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }


    /**
     * 入职-审核意见确认上传
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryReview(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookApprover", oaOfferLetterDTO.getApprover());
            taskService.complete(task.getId(), map);
            //todo 写入审核意见数据表
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-审核意见确认上传失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-审批通过
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryApprovePass(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookApprove", "同意");
            if (oaOfferLetterDTO.getIdentityType().equals(OaIdentityTypeEnum.IMPORTANT.getCode())) {
                map.put("isImportant", "是");
                map.put("hrBook", oaOfferLetterDTO.getCreator());
            }
            if (oaOfferLetterDTO.getIdentityType().equals(OaIdentityTypeEnum.NOT_IMPORTANT.getCode())) {
                map.put("isImportant", "不是");
                map.put("hrOrder", oaOfferLetterDTO.getCreator());

            }
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
            //启动入职工作流

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-审批通过失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-审批不通过
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryApproveRefuse(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("hr", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-审批不通过操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 新增任命书
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData newBook(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookReviewer", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增任命书操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-任命书审核意见确认上传
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryBookReview(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookApprove", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //TODO 保存审核意见

            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-任命书审核意见确认上传操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-任命书审批通过
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryBookApprovePass(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookApproved", "通过");
            map.put("hrOrder", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-任命书审批通过操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-任命书审批驳回
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryBookApproveRefuse(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("bookApproved", "驳回");
            map.put("hrBook", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-任命书审批驳回操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     *
     * 入职-人员安排通知单
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryPersonLetter(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("checkIn", oaOfferLetterDTO.getReceiver().getName());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-人员安排通知单操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-报到
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryPersonCheckIn(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            map.put("confirmCheckIn", oaOfferLetterDTO.getCreator());
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("入职-报到操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }

    /**
     * 入职-确认报到
     * @param oaOfferLetterDTO
     * @return
     */
    @Override
    public JsonData entryCheckIn(OaOfferLetterDTO oaOfferLetterDTO) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskService.createTaskQuery().processInstanceId(oaOfferLetterDTO.getProcessId()).singleResult();
        if (task == null) {
            return JsonData.buildError("流程不存在。");
        }
        try {
            taskService.complete(task.getId(), map);
            //更新状态
            OaOfferLetterDO offerLetterDO = CommonUtil.dtoTransfer(oaOfferLetterDTO, OaOfferLetterDO.class);
            offerLetterDO.setStatus(OaOfferLetterStatusTpyeEnum.HR_CONFIRMED.getCode());
            oaOfferLetterMapper.updateById(offerLetterDO);
            //新增员工数据
            OaEmployeeDO employeeDO = new OaEmployeeDO();
            employeeDO.setName(oaOfferLetterDTO.getName());
            employeeDO.setTelephone(oaOfferLetterDTO.getTelephone());
            employeeDO.setEmail(oaOfferLetterDTO.getEmail());
            employeeDO.setDept(oaOfferLetterDTO.getDept());
            employeeDO.setPosition(oaOfferLetterDTO.getPosition());
            employeeDO.setPost(oaOfferLetterDTO.getPost());
            employeeDO.setCreatedTime(new Date());
            oaEmployeeService.save(employeeDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("职-任命书审核意见确认上传操作失败{} {}", task.getId(), task.getAssignee());
        }
        return JsonData.buildSuccess();
    }



}
